package com.example.graphic.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.example.graphic.AI.ClassifyRequest;
import com.example.graphic.config.OssConfig;
import com.example.graphic.entity.Image;
import com.example.graphic.mapper.ImageMapper;
import com.example.graphic.service.OssService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class OssServiceImpl implements OssService {

    private static final Logger logger = LoggerFactory.getLogger(OssServiceImpl.class);

    @Autowired
    private OssConfig ossConfig;
    @Autowired
        private ImageMapper imageMapper;

    @Override
    public List<String> uploadFiles(MultipartFile[] files) {
        List<String> fileUrls = new ArrayList<>();
        OSS ossClient = null;
        try {
            // 创建OSSClient实例
            ossClient = new OSSClientBuilder().build(
                    ossConfig.getEndpoint(),
                    ossConfig.getAccessKeyId(),
                    ossConfig.getAccessKeySecret());
            for (MultipartFile file : files) {
                try {
                    // 生成对象名称
                    String objectName = generateObjectName(file.getOriginalFilename());
                    // 获取文件输入流
                    InputStream inputStream = file.getInputStream();
                    // 创建PutObjectRequest对象
                    PutObjectRequest putObjectRequest = new PutObjectRequest(
                            ossConfig.getBucketName(), objectName, inputStream);
                    // 上传文件
                    ossClient.putObject(putObjectRequest);
                    // 返回文件在 OSS 上的访问路径
                    String fileUrl = "https://"
                            + ossConfig.getBucketName()
                            + "."
                            + ossConfig.getEndpoint().replace("https://", "")
                            + "/"
                            + objectName;
                    fileUrls.add(fileUrl);

                } catch (IOException e) {
                    logger.error("文件读取失败", e);
                } catch (Exception e) {
                    logger.error("文件上传失败", e);
                }
            }
        } finally {
            // 关闭OSSClient
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return fileUrls;
    }

    /**
     * 生成唯一的对象名称
     *
     * @param originalFilename 原始文件名
     * @return 生成后的对象名称
     */
    private String generateObjectName(String originalFilename) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dir = simpleDateFormat.format(new Date()) + "/";
        String uuid = UUID.randomUUID().toString().replace("-", "");
        return dir + uuid + "_" + originalFilename;
    }
}